//
//  APIHistory.m
//  Bracelet
//
//  Created by betta on 2/16/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIHistory.h"
#import "FileReadAndWrite.h"

#define FILENAMECACHE_APIWORKOUTHISTORY @"fileName_apiWorkoutHistory"

@implementation APIOneHistory

-(instancetype)initApiFromOneHistoryFromDic:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        self.histroyEachSunday = [dic objectForKey:APIKEYEACHSUNDAY];
        self.historyPercent = [dic objectForKey:@"precent"];
        self.historyRunMetre = [dic objectForKey:APIKEYRUN_METRE];
    }
    return self;

}
+(instancetype)apiFromHistoryFromDictionay:(NSDictionary*)history
{
    return [[APIOneHistory alloc]initApiFromOneHistoryFromDic:history];
}

@end
@implementation APIHistory


-(instancetype) initFromDictionaryPaper:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        self.html = [dic objectForKey:APIKEYHTML];
//        self.arraySundayList = [dic objectForKey:APIKEYEACHSUNDAYLIST];
//        NSArray *arrayData = [dic objectForKey:APIKEYEDATALIST];
//        _arrayHistroyDataList = [[NSMutableArray alloc]init];
//        for (NSDictionary *dicTemp in arrayData) {
//           APIOneHistory *oneHisitory = [APIOneHistory apiFromHistoryFromDictionay:dicTemp];
//            [self.arrayHistroyDataList addObject:oneHisitory];
//        }
    }
    return self;
}


+(instancetype)apiFromHistoryFromDictionay:(NSDictionary*)history
{    
    return [[APIHistory alloc]initFromDictionaryPaper:history];
}

+(void)setApiRelativePosition:(NSString*)relativePostion
                getTheHistroy:(void (^)(APIHistory *outRank))successBlock
                      failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strUserId = [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};

     [[APIConnection shareInstance]post:relativePostion
                                   data:nil
                                 header:headerData
                               netError:^(NSError *error) {
        failureBlock(error);
                                   
       NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIWORKOUTHISTORY];
       if (dicCanch) {
           NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
           successBlock([APIHistory apiFromHistoryFromDictionay:dicSuccess]);
       }

    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            successBlock([APIHistory apiFromHistoryFromDictionay:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIWORKOUTHISTORY andData:dictionaryTemp];
        }
        else
        {
            failureBlock(nil);
        }
    }];
}

@end
